package com.yuju.controller.accounts;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.account.enums.AccountReportReturnCodeEnum;
import com.yuju.account.model.AccountReportModel;
import com.yuju.account.model.GuestBalanceModel;
import com.yuju.account.service.IAccountReportService;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.util.Utils;
import com.yuju.vo.account.AccountReportVO;

/**
 * 类名：AccountReportController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年12月21日 上午11:06:50  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("accountReport")
public class AccountReportController extends BaseController<AccountReportVO, AccountReportModel> {
	
	@Autowired
	private IAccountReportService accountReportService;
	
	@RequestMapping("guestBalanceSumRpt")
	@ResponseBody
	public Result guestBalanceSumRpt(@RequestBody JSONObject obj){
		AccountReportVO accountReportVO = obj.toJavaObject(AccountReportVO.class);
		GuestBalanceModel model = accountReportService.guestBalanceSumRpt(convert(accountReportVO));
		Result result = new Result();
		if(Utils.isNotNull(model)){
			result.setData(model);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_SUM_SUCCESS.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_SUM_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_SUM_FAILURE.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_SUM_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("getGuestBalanceReport")
	@ResponseBody
	public Result getGuestBalanceReport(@RequestBody JSONObject obj){
		AccountReportVO accountReportVO = obj.toJavaObject(AccountReportVO.class);
		List<GuestBalanceModel> list = null;
		PageModel pageModel =null;
		if(Utils.isNotNull(accountReportVO.getPage())){
			pageModel = accountReportService.guestBalanceReportPage(convert(accountReportVO), accountReportVO.getPage(), accountReportVO.getPageSize());
		}else{
			list = accountReportService.guestBalanceReport(convert(accountReportVO));
		}
		Result result = new Result();
		if(Utils.isNotNull(list)){
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_GUEST_SUCCESS.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_GUEST_SUCCESS.getDesc());
		}else if(Utils.isNotNull(pageModel)){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_GUEST_SUCCESS.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_GUEST_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_GUEST_FAILURE.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_GUEST_FAILURE.getDesc());
		}
		
		return result;
	}
	
	/**
	 * 功能：订单账务报表接口<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月21日 上午11:08:11 <br/>
	 */
	@RequestMapping("listOrderAndCount")
	@ResponseBody
	public Result listOrderAndCount(@RequestBody JSONObject obj){
		AccountReportVO accountReportVO = obj.toJavaObject(AccountReportVO.class);
		Map<String, String> fail = FieldValidator.validateAll(accountReportVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		PageModel pageModel = accountReportService.listOrderAndCount(convert(accountReportVO), accountReportVO.getPage(), accountReportVO.getPageSize());
		Result result = new Result();
		if(Utils.isNotNull(pageModel) && pageModel.getTotal()>0){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_QUERY_SUCCESS.getDesc());
		}else if(Utils.isNotNull(pageModel) && (pageModel.getTotal()<=0 || Utils.isNotNull(pageModel.getTotal()))){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_NOTHING.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_NOTHING.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_QUERY_FAILURE.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月22日 上午10:38:09 <br/>
	 */
	@RequestMapping("listMemberOrderAndCount")
	@ResponseBody
	public Result listMemberOrderAndCount(@RequestBody JSONObject obj){
		AccountReportVO accountReportVO = obj.toJavaObject(AccountReportVO.class);
		Map<String, String> fail = FieldValidator.validateAll(accountReportVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		PageModel pageModel = accountReportService.listMemberOrderAndCount(convert(accountReportVO), accountReportVO.getPage(), accountReportVO.getPageSize());
		Result result = new Result();
		if(Utils.isNotNull(pageModel) && pageModel.getTotal()>0){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_QUERY_SUCCESS.getDesc());
		}else if(Utils.isNotNull(pageModel) && (pageModel.getTotal()<=0 || Utils.isNotNull(pageModel.getTotal()))){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_NOTHING.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_NOTHING.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AccountReportReturnCodeEnum.ACCOUNTREPORT_QUERY_FAILURE.getCode());
			result.setReturnMsg(AccountReportReturnCodeEnum.ACCOUNTREPORT_QUERY_FAILURE.getDesc());
		}
		return result;
	}

}
